/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class FederatedSearchTests {

    static testMethod void myUnitTest() {
		// Insert a test demo component to ensure there is some data to be found
		Case dummyData = new Case(subject='test');
		insert dummyData;
	
		// use 'keyword' only
		PageReference testPage = Page.federatedSearch;
		FederatedSearchController.testMode = true;
		FederatedSearchController controller = new FederatedSearchController();
		controller.Keywords = 'salesforce.com';
		controller.doSearch();
		
		// null search
		testPage = Page.federatedSearch;
		controller = new FederatedSearchController();
		controller.doSearch();
		system.assert(controller.getNoResults(),'There should not be any results for a null search');
	
		// test that url parameters works
		testPage = Page.federatedSearch;
		testPage.getParameters().put('keywords','salesforce.com');
		Test.setCurrentPage(testPage);
		controller = new FederatedSearchController();
		controller.doSearch();
		controller.continueProcessing();		  
		System.debug(controller.bookmarkUrl);
		System.debug(controller.caseResults);
		System.debug(controller.documentResults);
		System.debug(controller.solutionResults);
		System.debug(controller.yahooResults);
		System.debug(controller.keywords);
		System.debug(controller.numCaseResults);
		System.debug(controller.numDocumentResults);
		System.debug(controller.numSolutionResults);
		System.debug(controller.numYahooResults);
		System.debug(controller.showBookmarkMessage);
		System.debug(controller.showResults);		
		System.debug(controller.link());	
    }
    
    static testMethod void TestYahoo() {
		String testText = '<?xml version="1.0" encoding="UTF-8"?>'+
			'<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:srch" xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/WebSearchService/V1/WebSearchResponse.xsd" type="web" totalResultsAvailable="608000" totalResultsReturned="10" firstResultPosition="1" moreSearch="/WebSearchService/V1/webSearch?query=salesforce.com&amp;appid=7.zX80HV34Fh.Mapw46SoYG8e_jQcBYgrbY1DC9aT1uw.E7VID8L4._B_4ER_dihckwMohY-&amp;region=us">'+
			'<Result><Title>CRM - The Leader In Software-as-a-Service (SaaS) - salesforce.com CRM ...</Title><Summary>Provides online CRM (customer relationship management) software.</Summary><Url>http://www.salesforce.com/</Url><ClickUrl>http://uk.wrs.yahoo.com/_ylt=A0Je5VOpNFBJ_mYBDUXdmMwF;_ylu=X3oDMTE1OTBoN2kzBGNvbG8DdwRsA1dTMQRwb3MDMQRxdANkcQRzZWMDc3IEdnRpZAM-/SIG=11et0oc9k/EXP=1230079529/**http%3A//www.salesforce.com/</ClickUrl><DisplayUrl>www.salesforce.com/</DisplayUrl><ModificationDate>1229932800</ModificationDate><MimeType>text/html</MimeType>'+
			'<Cache><Url>http://uk.wrs.yahoo.com/_ylt=A0Je5VOpNFBJ_mYBDkXdmMwF;_ylu=X3oDMTBwOHA5a2tvBGNvbG8DdwRwb3MDMQRzZWMDc3IEdnRpZAM-/SIG=173loprua/EXP=1230079529/**http%3A//66.218.69.11/search/cache%3Fei=UTF-8%26appid=7.zX80HV34Fh.Mapw46SoYG8e_jQcBYgrbY1DC9aT1uw.E7VID8L4._B_4ER_dihckwMohY-%26query=salesforce.com%26u=www.salesforce.com/%26d=J76E8EfiSAfQ%26icp=1%26.intl=us</Url><Size>57835</Size></Cache>'+
			'</Result></ResultSet>';
		XmlStreamReader reader = new XmlStreamReader(testText);
		List<YahooSearch> yahooResults = new List<YahooSearch>();
		
	    // Read through the XML
	    while(reader.hasNext()) {
			System.debug('Event Type:' + reader.getEventType());
			if (reader.getEventType() == XmlTag.START_ELEMENT) {
				System.debug(reader.getLocalName());
	            if ('Result' == reader.getLocalName()) {
	                yahooResults.add(new YahooSearch(reader)); 
	            }
			}
			reader.next();
	    }	
    }
}